from langchain.output_parsers import CommaSeparatedListOutputParser
import os

from langchain_community.chat_models import ChatTongyi
from langchain_core.prompts import ChatPromptTemplate

os.environ["DASHSCOPE_API_KEY"] = "sk-9d8f1914800e497f8717144e860f99bc"
llm = ChatTongyi()
output_parser = CommaSeparatedListOutputParser()
# Your response should be a list of comma separated values, eg: `foo, bar, baz`

format_instructions = output_parser.get_format_instructions()
print(format_instructions)

prompt = ChatPromptTemplate.from_messages(
    [
        ("system", "{parser_instructions}"),
        ("human", "列出{cityName}的{viewPointNum}个著名景点。")
    ])
output_parser = CommaSeparatedListOutputParser()
parser_instructions = output_parser.get_format_instructions()
# 查看解析器的指令内容
print(parser_instructions)

# final_prompt = prompt.invoke({"cityName": "南京", "viewPointNum": 3, "parser_instructions": parser_instructions})
final_prompt = prompt.format(cityName="南京", viewPointNum=3, parser_instructions= parser_instructions)

from langchain.chains import LLMChain

reponse = llm.invoke(final_prompt)
print(reponse.content)

ret = output_parser.invoke(reponse)
print(ret)
